// 通用类型定义
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  success: boolean
  timestamp: string
}

export interface PaginationParams {
  page: number
  pageSize: number
  total?: number
}

export interface PaginationResponse<T> {
  list: T[]
  pagination: {
    page: number
    pageSize: number
    total: number
    totalPages: number
  }
}

// 路由相关类型
export interface RouteMetaInfo {
  title?: string
  description?: string
  keywords?: string
  requiresAuth?: boolean
  roles?: string[]
  permissions?: string[]
  keepAlive?: boolean
  showInMenu?: boolean
  icon?: string
  order?: number
}

// 菜单类型
export interface MenuItem {
  id: string
  title: string
  path: string
  icon?: string
  children?: MenuItem[]
  meta?: RouteMetaInfo
  external?: boolean
}

// 表单相关类型
export interface FormRule {
  required?: boolean
  message?: string
  trigger?: string | string[]
  type?: string
  pattern?: RegExp
  min?: number
  max?: number
  validator?: (rule: any, value: any, callback: any) => void
}

export interface FormRules {
  [key: string]: FormRule | FormRule[]
}

// 表格相关类型
export interface TableColumn {
  prop: string
  label: string
  width?: string | number
  minWidth?: string | number
  fixed?: boolean | 'left' | 'right'
  sortable?: boolean
  formatter?: (row: any, column: any, cellValue: any, index: number) => string
  align?: 'left' | 'center' | 'right'
  headerAlign?: 'left' | 'center' | 'right'
  showOverflowTooltip?: boolean
  resizable?: boolean
}

export interface TableConfig {
  columns: TableColumn[]
  data: any[]
  loading?: boolean
  pagination?: PaginationParams
  selection?: boolean
  stripe?: boolean
  border?: boolean
  size?: 'large' | 'default' | 'small'
  height?: string | number
  maxHeight?: string | number
}

// 文件上传类型
export interface UploadFile {
  id?: string
  name: string
  size: number
  type: string
  url?: string
  status?: 'ready' | 'uploading' | 'success' | 'error'
  progress?: number
  response?: any
  error?: string
}

export interface UploadConfig {
  action: string
  method?: 'POST' | 'PUT'
  headers?: Record<string, string>
  data?: Record<string, any>
  name?: string
  withCredentials?: boolean
  multiple?: boolean
  accept?: string
  maxSize?: number
  maxCount?: number
  beforeUpload?: (file: File) => boolean | Promise<boolean>
  onProgress?: (progress: number, file: UploadFile) => void
  onSuccess?: (response: any, file: UploadFile) => void
  onError?: (error: any, file: UploadFile) => void
}

// 通知类型
export interface NotificationItem {
  id: string
  title: string
  content: string
  type: 'info' | 'success' | 'warning' | 'error'
  read: boolean
  createdAt: string
  actions?: {
    label: string
    action: () => void
  }[]
}

// 主题配置类型
export interface ThemeConfig {
  primaryColor: string
  successColor: string
  warningColor: string
  errorColor: string
  infoColor: string
  textColor: string
  backgroundColor: string
  borderColor: string
  borderRadius: string
  fontSize: {
    small: string
    default: string
    large: string
  }
  spacing: {
    small: string
    default: string
    large: string
  }
}

// 系统配置类型
export interface SystemConfig {
  siteName: string
  siteDescription: string
  siteKeywords: string
  logo: string
  favicon: string
  copyright: string
  version: string
  apiBaseUrl: string
  uploadUrl: string
  staticUrl: string
  enableDebug: boolean
  enableAnalytics: boolean
  analyticsId?: string
}

// 错误类型
export interface ErrorInfo {
  code: string | number
  message: string
  details?: any
  stack?: string
  timestamp: string
  url?: string
  userAgent?: string
}

// 日志类型
export interface LogEntry {
  id: string
  level: 'debug' | 'info' | 'warn' | 'error'
  message: string
  data?: any
  timestamp: string
  source?: string
  userId?: string
}

// 统计数据类型
export interface StatisticsData {
  label: string
  value: number | string
  unit?: string
  trend?: 'up' | 'down' | 'stable'
  trendValue?: number
  color?: string
  icon?: string
}

// 图表数据类型
export interface ChartData {
  labels: string[]
  datasets: {
    label: string
    data: number[]
    backgroundColor?: string | string[]
    borderColor?: string | string[]
    borderWidth?: number
    fill?: boolean
  }[]
}

export interface ChartOptions {
  responsive?: boolean
  maintainAspectRatio?: boolean
  plugins?: {
    legend?: {
      display?: boolean
      position?: 'top' | 'bottom' | 'left' | 'right'
    }
    tooltip?: {
      enabled?: boolean
    }
  }
  scales?: {
    x?: {
      display?: boolean
      title?: {
        display?: boolean
        text?: string
      }
    }
    y?: {
      display?: boolean
      title?: {
        display?: boolean
        text?: string
      }
      beginAtZero?: boolean
    }
  }
}

// 搜索相关类型
export interface SearchParams {
  keyword?: string
  category?: string
  tags?: string[]
  dateRange?: [string, string]
  sortBy?: string
  sortOrder?: 'asc' | 'desc'
  filters?: Record<string, any>
}

export interface SearchResult<T> {
  items: T[]
  total: number
  page: number
  pageSize: number
  totalPages: number
  searchTime: number
  suggestions?: string[]
}

// 导出相关类型
export interface ExportConfig {
  format: 'excel' | 'csv' | 'pdf'
  filename?: string
  columns?: string[]
  filters?: Record<string, any>
  template?: string
}

export interface ImportConfig {
  format: 'excel' | 'csv'
  template?: string
  mapping?: Record<string, string>
  validation?: {
    required?: string[]
    unique?: string[]
    format?: Record<string, RegExp>
  }
}

// 权限相关类型
export interface Permission {
  id: string
  name: string
  code: string
  description?: string
  resource: string
  action: string
  conditions?: Record<string, any>
}

export interface Role {
  id: string
  name: string
  code: string
  description?: string
  permissions: Permission[]
  isDefault?: boolean
  createdAt: string
  updatedAt: string
}

// 工作流相关类型
export interface WorkflowNode {
  id: string
  type: 'start' | 'end' | 'task' | 'decision' | 'parallel'
  name: string
  description?: string
  position: { x: number; y: number }
  config?: Record<string, any>
  inputs?: string[]
  outputs?: string[]
}

export interface WorkflowEdge {
  id: string
  source: string
  target: string
  label?: string
  condition?: string
}

export interface Workflow {
  id: string
  name: string
  description?: string
  version: string
  status: 'draft' | 'active' | 'inactive'
  nodes: WorkflowNode[]
  edges: WorkflowEdge[]
  variables?: Record<string, any>
  createdAt: string
  updatedAt: string
}

// 消息相关类型
export interface Message {
  id: string
  type: 'text' | 'image' | 'file' | 'system'
  content: string
  sender: {
    id: string
    name: string
    avatar?: string
    type: 'user' | 'system' | 'bot'
  }
  timestamp: string
  read?: boolean
  metadata?: Record<string, any>
}

export interface Conversation {
  id: string
  title: string
  participants: {
    id: string
    name: string
    avatar?: string
    role: string
  }[]
  messages: Message[]
  lastMessage?: Message
  unreadCount: number
  createdAt: string
  updatedAt: string
}

// 事件类型
export interface EventData {
  type: string
  payload: any
  timestamp: string
  source?: string
}

export interface EventHandler {
  (event: EventData): void | Promise<void>
}

// 缓存类型
export interface CacheItem<T = any> {
  key: string
  value: T
  expireTime?: number
  createdAt: number
  accessCount: number
  lastAccessed: number
}

export interface CacheConfig {
  maxSize?: number
  defaultTTL?: number
  checkInterval?: number
  enableStats?: boolean
}

// 设备信息类型
export interface DeviceInfo {
  type: 'desktop' | 'tablet' | 'mobile'
  os: string
  browser: string
  version: string
  screen: {
    width: number
    height: number
    ratio: number
  }
  support: {
    touch: boolean
    webgl: boolean
    webrtc: boolean
    geolocation: boolean
  }
}

// 地理位置类型
export interface Location {
  latitude: number
  longitude: number
  accuracy?: number
  altitude?: number
  altitudeAccuracy?: number
  heading?: number
  speed?: number
  timestamp: number
}

export interface Address {
  country?: string
  province?: string
  city?: string
  district?: string
  street?: string
  detail?: string
  postalCode?: string
}